/**
 * 
 */
package com.et114.modules.org.dao;

import com.et114.components.acegi.Constants;
import com.et114.core.common.AppContext;
import com.et114.core.dao.impl.BaseDAOImpl;
import com.et114.core.exception.CheckException;
import com.et114.modules.org.vo.FuncRoleRelVo;
import com.et114.modules.org.vo.FunctionVo;

import java.util.List;
/**
 * @author guanhw
 */
public class FunctionDAO extends BaseDAOImpl {
	
	/**
	 * @param vo
	 * @return
	 * @throws CheckException
	 */
	public List getFuncs ( FunctionVo  vo ) throws CheckException { 
		return queryList(  vo , "getFunctions" ) ;
	}
	
	public FunctionVo getFunc( Long funcId )  throws CheckException { 
		return ( FunctionVo )load ( funcId , "getFunction" ) ;
	}
	
	public FunctionVo getFuncByPath( String path ) throws CheckException {
		return ( FunctionVo )load ( path , "getFuncByPath" ) ;
	}
	
	public List getFuncs ( ) throws CheckException { 
		return getFuncs(  new FunctionVo()  ) ;
	}
	
	public List getFunctionsWithCountRel( FunctionVo  vo  ) throws CheckException {  
		return queryList(  vo , "getFunctionsWithCountRel" ) ;
	}
	
	public List loadFunctionsByIds ( String[] funcIds ) throws CheckException { 
		if ( funcIds != null && funcIds.length > 0 ) {
			FunctionVo  vo  = new FunctionVo(  ) ; 
			vo.setFuncids ( funcIds ) ;
			return queryList( vo , "loadFunctionsByIds" ) ; 
		}
		else return new java.util.ArrayList( ) ; 
	}
	
	public List getRoots() throws CheckException {
		return queryList( new FunctionVo() , "getRoots"  ) ;
	}
	
	public List getRootsWithCountRel( ) throws CheckException {
		return queryList( new FunctionVo() , "getRootsWithCountRel"  ) ;
	}
	
	public List getRolesByFunc( FunctionVo  vo )  throws CheckException { 
		return queryList(  vo , "getRolesByFunction" ) ;
	}
	
	public List getGreaterRolesByFunction ( FunctionVo  vo ) throws CheckException {  
		return queryList( vo , "getGreaterRolesByFunction" ) ;
	}
	
	public Long countgetGreaterRolesByFunction ( FunctionVo  vo ) throws CheckException {
		return getCount( vo , "countgetGreaterRolesByFunction" ) ;
	}
	
	public List getGreaterRolesByFunctionNPage ( FunctionVo  vo ) throws CheckException {  
		return queryList( vo , "getGreaterRolesByFunctionNPage" ) ;
	}	
	
	public List getUsersByFunc(  FunctionVo  vo  ) throws CheckException {  
		return queryList(  vo , "getUsersByFunction" ) ;
	}
	
	public List getTitlesByFunc(  FunctionVo  vo  ) throws CheckException {  
		return queryList(  vo , "getTitlesByFunction" ) ;
	}	
	
	public Long countgetTitlesByFunction( FunctionVo  vo  ) throws CheckException {  
		return getCount( vo , "countgetTitlesByFunction" ) ;
	}
	
	public void addFuncLog( FunctionVo log  ) throws CheckException { 
		insert( log , "insertLogFunction" ) ;
	}
	
	
	public Long addFunc( FunctionVo function ) throws CheckException { 
		Long id = ( Long )insert ( function , "insertFunction" ) ;
		if ( AppContext.isHasLog (  ) ) {		
			function.setOperType ( Constants.LOG_OPER_NEW ) ; 
			addFuncLog( function ) ;
		} logger.info ( "func......" + id ) ;
		return id ; 
	}	
	
	public int updateFunc( FunctionVo function ) throws CheckException { 
		int result  = update ( function , "updateFunction" ) ;
		if ( AppContext.isHasLog (  ) ) {		
			function.setOperType ( Constants.LOG_OPER_UPDATE ) ; 
			addFuncLog( function ) ;
		}
		return result ; 
	}
	
	public void deleteFuncById( Long funcId , Long operUser ) throws CheckException { 
		if ( AppContext.isHasLog (  ) ) {		
			FunctionVo log = getFunc( funcId ) ;
			log.setOperUser ( operUser ) ;
			log.setOperType ( Constants.LOG_OPER_REMOVE ) ; 
			insert( log , "insertLogFunction" ) ; 		
		}
		delete ( funcId , "deleteFunctionById" ) ;
	}
	
	public void deleteFuncByPid( Long ffuncId ,   Long operUser ) throws CheckException { 
		if ( AppContext.isHasLog (  ) ) {		
			FunctionVo log = getFunc( ffuncId ) ;
			log.setOperUser ( operUser ) ;			
			log.setOperType ( Constants.LOG_OPER_REMOVE ) ; 
			insert( log , "insertLogFunction" ) ; 			
		}
		delete ( ffuncId , "deleteFunctionByPid" ) ;
	}
	
	public int countFuncs( FunctionVo func ) throws CheckException {
		return getCount ( func , "countFuns" ).intValue ( ) ;
	}
	
	
	public Long maxFuncNoByParentId( Long pid ) throws CheckException { 
		return ( Long )sqlMapClientTemplate.queryForObject( "maxFuncNoByParentId" , pid ) ; 
	}

	public Long getcountFuns( FunctionVo func ) throws CheckException {
		return getCount ( func , "getcountFuns" );
	}
	
	
	public List getFunctionsByUserAllPrincipals( Long userId ) throws CheckException { 
		return queryList( userId , "getFunctionsByUserAllPrincipals" ) ;
	}
	
	public List getRolesByFuncId4f( FuncRoleRelVo funcRoleRel ) throws CheckException {
		return queryList( funcRoleRel , "getRolesByFuncId4f" ) ;
	}
	
	public Long countgetRolesByFuncId4f( FuncRoleRelVo funcRoleRel )  throws CheckException { 
		return getCount ( funcRoleRel , "countgetRolesByFuncId4f" );
	}
	
	public List getRolesByFunc4fNp( FuncRoleRelVo funcRoleRel ) throws CheckException { 
		return queryList( funcRoleRel , "getRolesByFunc4fNp" ) ;
	}
	
	public List getFuncByRole4fNp( FuncRoleRelVo funcRoleRel ) throws CheckException { 
		return queryList( funcRoleRel , "getFuncByRole4fNp" ) ;
	}	
	
	
	public List getFuncsByPath( String path )  throws CheckException { 
		return queryList( path , "getFuncsByPath" ) ;
	}
	
	public void createFuncRoleRel( FuncRoleRelVo funcRoleRel ) throws CheckException {
		insert( funcRoleRel , "insertFuncRoleRel" ) ;
		if ( AppContext.isHasLog (  ) ) {		
			funcRoleRel.setOperType ( Constants.LOG_OPER_NEW ) ; 
			insertLogFuncRoleRel( funcRoleRel ) ;
		}
	}
	
	public void deleteFuncRoleRel(  FuncRoleRelVo funcRoleRel ) throws CheckException { 
		if ( AppContext.isHasLog (  ) ) {		
			funcRoleRel.setOperType ( Constants.LOG_OPER_REMOVE ) ; 
			insertLogFuncRoleRel( funcRoleRel ) ;
		}		
		delete ( funcRoleRel , "deleteFuncRoleRel" ) ;
	}
	
	public void insertLogFuncRoleRel( FuncRoleRelVo funcRoleRel ) throws CheckException {  
		insert( funcRoleRel , "insertLogFuncRoleRel" ) ;
	}
}
